<div class="filter-bar">
<form action="__CONTROLLER__/publish" method="post" >
	<div class="view">
		<div class="row clearfix" style="margin-top:5px;" id="prog">
			<div class="col-md-12 column ui-sortable">
				<div class="col-sm-12">
					<div class="progress progress-striped active">
					   <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
					      <span class="value">0%</span> <!-- sr-only -->
					   </div>
					</div>
				</div>
			</div>
		</div>
		<div class="row clearfix">
			<div class="col-md-3 column ui-sortable">
			    <label class="col-sm-3 control-label"  style="margin-top:6px;">从</label>
			    <div class="col-sm-7">
				    <input type="text" id="start-time" class="form-control" name="startDate" value="{$timeRange[0]|date='Y-m-d H:i:s',###}" placeholder="" datatype="*">
			    </div>
			</div>
			<div class="col-md-3 column ui-sortable">
			    <label class="col-sm-3 control-label" style="margin-top:6px;">发布至</label>
			    <div class="col-sm-7">
	               <input type="text" id="end-time" class="form-control" name="endDate" value="{$timeRange[1]|date='Y-m-d H:i:s',###}" placeholder="" datatype="*">
			    </div>
			</div>
			<div class="col-md-3 column ui-sortable">
			    <div class="col-sm-9">
			       <input type="hidden" name="term_id" value="{$termId}" />
			       <a class="btn btn-info" href="javascript:return false;" id="view-lately">最近投票情况</a>
			       <input id="btn-publish" type="button" class="btn btn-info submit" value="{$progress?'继续发布':'发布'}" />
			    </div>
			</div>
		</div>
	</div>
</form>
</div>
<hr/>

<table class="table table-hover">
	<thead>
		<tr>
			<th>ID</th>
			<th>发布截止至</th>
			<th>发布日期</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody>
		<volist name="driverSnapLists" id="vo">
			<tr>
				<td>{$vo.id}</td>
				<td>{$vo.end_time|date='Y-m-d H:i:s',###}</td>
				<td>{$vo.create_time|date='Y-m-d H:i:s',###}</td>
				<td>
					<a class="btn btn-primary btn-xs" href="{:U('DriverSnap/snapRecord', 'term_id='.$termId.'&id='.$vo['id'])}">查看Top500</a>
				</td>
			</tr>
		</volist>
	</tbody>
</table>
<div class="pagination">{$pagination}</div>

<!-- datetimepicker -->
<link rel="stylesheet" href="__PUBLIC__/Style/Common/datetimepicker/bootstrap-datetimepicker.min.css">
<script src="__PUBLIC__/Style/Common/datetimepicker/bootstrap-datetimepicker.min.js"></script>
<script src="__PUBLIC__/Style/Common/datetimepicker/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script type="text/javascript">
  var termId = {$termId};
  var prog = '{$progress}'*1;
  
  function Progress(cfg) {
		this.init(cfg);
  }
	
  Progress.prototype.init = function(cfg) {
		this.cfg = cfg;
		this.p = 0;
		this.obj = $(cfg['bar']);
  }
	
  Progress.prototype.start = function() {
	var _this = this;
	this.timer = setInterval(function() {
	  	$.post('__CONTROLLER__/getProgress', {'term_id':termId}, function(data) {
	  		var progress = data.extra.progress*1;
	    	_this.set(progress, data.extra);
	    	if ( progress >= 100 ) {
	    		_this.stop();
	    		_this.hide();
	    	}
	    });
	}, 1000);
  }
	
  Progress.prototype.stop = function() {
	if ( this.timer ) {
		clearInterval(this.timer);
	}
	this.timer = 0;
  }
	
  Progress.prototype.set = function( value, extra ) {
	this.obj.css('width', value+"%");
	this.obj.find(".value").html(value+"% ("+extra.completed+"/"+extra.total+")");
  }
	
  $(function() {
	  
	if( !prog ) {
		$("#prog").hide();
	}
	
    $("#start-time").datetimepicker({"language": "zh-CN"});
    $("#end-time").datetimepicker({"language": "zh-CN"});
    
    $("#btn-publish").click(function() {
    	var progress = new Progress({'bar':'.progress .progress-bar'});
    	$(this).attr('disabled', 'disabled');
    	$("#prog").show();
    	(function run() {
    		try{
            	$.post('__CONTROLLER__/publish', {'startDate':$('#start-time').val(), 'endDate':$('#end-time').val(), 'term_id':termId}, function(data) {
            		if( data['error'] ) {
            			setTimeout(run, 500);
            		} else {
            			window.progress_finish = true;
            			progress.stop();
            			progress.set(100);
            			alert('发布成功');
            			$("#btn-publish").removeAttr('disabled');
            		}
            	});
    		} catch(e) {
    			setTimeout(run, 500);
    		}
    	})();
    	setTimeout(function() {
    		window.progress_finish || progress.start();
    	}, 2000 );
    });
    
    $("#view-lately").click(function() {
    	window.location.href = "__CONTROLLER__/viewVote?startDate="+$('#start-time').val()+"&endDate="+$('#end-time').val()+"&term_id={$termId}";
    });
    
  });
</script>